//
//  ByRegisterApiUser.m
//  oc_knowledge
//
//  Created by justiOS on 17/1/11.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByApiUser.h"
#define UPLOAD_ERROR_STRING_CODE -67
#define UPLOAD_ERROR_OTHER_CODE -76
@implementation ByApiUser


/** 登陆 */
+ (void)login:(ByNetParamsModel *)model
      success:(void(^)(ByNetBaseResult *result))success
      failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_UserAccount_loginByMobile";
    static NSInteger apiVersion = 103;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%.0lf%d", [NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];

    if (model.code) {
        [params setObject:model.code forKey:@"code"];
    }else{
         [params setObject:@"itboye" forKey:@"code"];
    }
    
    [params setObject:model.mobile forKey:@"mobile"];
    
    [params setObject:@"+86" forKey:@"country"];
    
    
    if (model.role) {
        [params setObject:model.role forKey:@"role"];
    }
    
    
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeDictionary
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           NSLog(@"******** 登录 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"登录成功";
           ByModelUser *model = [[ByModelUser alloc] initContentWithDic:responseObject];
           result.data = model;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           NSLog(@"******** 登录 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}


//手机号发送验证码
+ (void)sendCode:(ByNetParamsModel *)model
         success:(void(^)(ByNetBaseResult *result))success
         failure:(void(^)(ByNetBaseResult *result))failure{
    
    
    static NSString *apiType = @"By_SecurityCode_send";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];

    if (model.code_type) {
        [params setObject:model.code_type forKey:@"code_type"];
    }else{
        [params setObject:@"5" forKey:@"code_type"];
    }
    [params setObject:model.mobile forKey:@"mobile"];
    if ([model.mobile isEqualToString:@"13136167925"]) {
        [params setObject:@"alert" forKey:@"send_type"];
    }else{
         [params setObject:@"sms_juhe" forKey:@"send_type"];
    }
    [params setObject:@"+86" forKey:@"country"];
    [params setObject:@"120" forKey:@"expire_time"];
    
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           NSLog(@" 注册验证码 = %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"注册成功";
           result.data = responseObject;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           CodeLog(@"******** 注册 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}





//用户个人资料修改
+ (void)userUpdate:(ByNetParamsModel *)model
           success:(void(^)(ByNetBaseResult *result))success
           failure:(void(^)(ByNetBaseResult *result))failure{
    
    
    static NSString *apiType = @"By_UserProfile_update";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    
    if (model.uid) {
        [params setObject:model.uid forKey:@"uid"];
    }
    
    if (model.nickname) {
        [params setObject:model.nickname forKey:@"nickname"];
    }
    
    if (model.sign) {
        [params setObject:model.sign forKey:@"sign"];
    }
    if (model.sex) {
        [params setObject:model.sex forKey:@"sex"];
    }
    if (model.realname) {
        [params setObject:model.realname forKey:@"realname"];
    }
    if (model.idnumber) {
        [params setObject:model.idnumber forKey:@"idnumber"];
    }
    if (model.birthday) {
        [params setObject:model.birthday forKey:@"birthday"];
    }
    if (model.hometown) {
        [params setObject:model.hometown forKey:@"hometown"];
    }
    if (model.nation) {
        [params setObject:model.nation forKey:@"nation"];
    }
    if (model.profession) {
        [params setObject:model.profession forKey:@"profession"];
    }
    if (model.works_cate) {
        [params setObject:model.works_cate forKey:@"works_cate"];
    }
    if (model.working_years) {
        [params setObject:model.working_years forKey:@"working_years"];
    }
    
    
    
    
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           CodeLog(@"******** 更新 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"更新成功";
           result.data = responseObject;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           CodeLog(@"******** 更新 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];

    
    
    
}





+ (void)loginSession:(ByNetParamsModel *)model
             success:(void(^)(ByNetBaseResult *result))success
             failure:(void(^)(ByNetBaseResult *result))failure{
    
    
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    
    if (model.uid) {
        [dictionary setObject:model.uid forKey:@"uid"];
    }
    
    if (model.auto_login_code) {
        [dictionary setObject:model.auto_login_code forKey:@"auto_login_code"];
    }
    
    static NSString *apiType = @"By_User_autoLogin";
    static NSInteger apiVersion = 100;
    int rand = arc4random() % 1000;
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:dictionary
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
          
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"添加成功";
           result.data = responseObject;
           result.notify_id = notifyID;
           if(success){
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure){
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
           
       }];
    
    
}

+ (void)suggestAdd:(ByNetModel *)model
           success:(void(^)(ByNetBaseResult *result))success
           failure:(void(^)(ByNetBaseResult *result))failure{
    
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    
    if (model.uid) {
        [dictionary setObject:model.uid forKey:@"uid"];
    }
    
    if (model.suggest_content) {
        [dictionary setObject:model.suggest_content forKey:@"suggest_content"];
    }
    
    if (model.tel) {
        [dictionary setObject:model.tel forKey:@"tel"];
    }else{
        [dictionary setObject:@"13136167925" forKey:@"tel"];
    }
    
    [dictionary setObject:@"test" forKey:@"name"];
    
    static NSString *apiType = @"By_Suggest_add";
    static NSInteger apiVersion = 100;
    int rand = arc4random() % 1000;
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:dictionary
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"添加成功";
           result.data = responseObject;
           result.notify_id = notifyID;
           if(success){
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure){
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
           
       }];
    
    
    
}


//消息查询接口
+ (void)messageQuery:(ByNetModel *)model
             success:(void(^)(ByNetBaseResult *result))success
             failure:(void(^)(ByNetBaseResult *result))failure{
    
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    
    if (model.uid) {
        [dictionary setObject:model.uid forKey:@"uid"];
    }
    
    [dictionary setObject:@"1" forKey:@"page_index"];
    [dictionary setObject:@"1000" forKey:@"page_size"];
    
    
    static NSString *apiType = @"By_Message_query";
    static NSInteger apiVersion = 100;
    int rand = arc4random() % 1000;
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeArray
     apiVerson:apiVersion
        params:dictionary
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           NSLog(@"消息 responseObject =  %@", responseObject);
           NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
           for (NSDictionary *dic in responseObject){
               
               ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
               [tempArray addObject:netModel];
           }
           
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"添加成功";
           result.data = tempArray;
           result.notify_id = notifyID;
           if(success){
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure){
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
           
       }];
    

}

@end
